package com.woniu.p2p.excelservice;
import com.woniu.p2p.entity.*;
import com.woniu.p2p.mapper.*;
import com.woniu.p2p.vo.AllInvestVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class InExcelProjectBreifServiceImpl implements InExcelProjectBreifService{

    @Autowired
    InvestMapper investMapper;
    @Autowired
    InvestRepayMapper investRepayMapper;
    @Autowired
    InvestStafguardMapper investStafguardMapper;
    @Autowired
    InvestCompanyMapper investCompanyMapper;
    @Autowired
    InvestRiskMapper investRiskMapper;
    @Autowired
    InvestTypeMapper investTypeMapper;
    @Autowired
    InvestStatusMapper investStatusMapper;

    @Override
    public void readExcel(AllInvestVo allInvestVo) {
        Integer investId = allInvestVo.getInvestId();
        //根据id查询项目,来验证项目是否已经存在
        Invest invest = investMapper.selectByPrimaryKey(investId);

        Integer investNumber = allInvestVo.getInvestNumber();
        String investName = allInvestVo.getInvestName();
        Long investStartpay = allInvestVo.getInvestStartpay();
        Double investGetyearmoney = allInvestVo.getInvestGetyearmoney();
        Integer investDay = allInvestVo.getInvestDay();
        Long investAmount = allInvestVo.getInvestAmount();
        String investDeadline = allInvestVo.getInvestDeadline();
        Date investReleasetime = allInvestVo.getInvestReleasetime();
        Date investPaytime = allInvestVo.getInvestPaytime();
        String investStafguard = allInvestVo.getInvestStafguard();
        Long investBalance = allInvestVo.getInvestBalance();
        String investSubsribe = allInvestVo.getInvestSubsribe();
        String investRemaindertime = allInvestVo.getInvestRemaindertime();
        Long investGetmonofyear = allInvestVo.getInvestGetmonofyear();

        String repayName = allInvestVo.getRepayName();
        String stafguardName = allInvestVo.getStafguardName();
        String companyName = allInvestVo.getCompanyName();
        Integer riskPingname = allInvestVo.getRiskPingname();
        String typeName = allInvestVo.getTypeName();
        String statusName = allInvestVo.getStatusName();
        if(invest==null){
            //如果项目没有那么就增加新的项目信息
            //判断增加还款方法
            InvestRepay allrepay = investRepayMapper.findAllByName(repayName);
            Integer repayid=0;
            if(allrepay==null){
                InvestRepay investRepay = new InvestRepay(repayName);
                investRepayMapper.insert(investRepay);
                InvestRepay allByName = investRepayMapper.findAllByName(repayName);
                Integer repayId = allByName.getRepayId();
                repayid=repayId;
            }else {
                Integer repayId = allrepay.getRepayId();
                repayid=repayId;
            }
            //判断增加保障机构
            InvestStafguard allStafguardByName = investStafguardMapper.findAllStafguardByName(stafguardName);
            Integer stafid=0;
            if(allStafguardByName==null){
                InvestStafguard investStafguard1 = new InvestStafguard(stafguardName);
                investStafguardMapper.insert(investStafguard1);
                InvestStafguard allStafguardByName1 = investStafguardMapper.findAllStafguardByName(stafguardName);
                Integer stafguardId = allStafguardByName1.getStafguardId();
                stafid=stafguardId;
            }else {
                Integer stafguardId = allStafguardByName.getStafguardId();
                stafid=stafguardId;
            }
            //判断增加公司名字
            InvestCompany allCompanyByCompanyName = investCompanyMapper.findAllCompanyByCompanyName(companyName);
            Integer companyid=0;
            if(allCompanyByCompanyName==null){
                InvestCompany investCompany = new InvestCompany(companyName);
                int insert = investCompanyMapper.insert(investCompany);
                InvestCompany allCompanyByCompanyName1 = investCompanyMapper.findAllCompanyByCompanyName(companyName);
                Integer companyId = allCompanyByCompanyName1.getCompanyId();
                companyid=companyId;
            }else {
                Integer companyId = allCompanyByCompanyName.getCompanyId();
                companyid=companyId;
            }
            //判断增加项目类型
            InvestType allTypeByName = investTypeMapper.findAllTypeByName(typeName);
            Integer typeid=0;
            if(allTypeByName==null){
                InvestType investType = new InvestType(typeName);
                investTypeMapper.insert(investType);
                InvestType allTypeByName1 = investTypeMapper.findAllTypeByName(typeName);
                Integer typeId = allTypeByName1.getTypeId();
                typeid=typeId;
            }else {
                Integer typeId = allTypeByName.getTypeId();
                typeid=typeId;
            }
            //判断增加项目状态
            InvestStatus allStatusByName = investStatusMapper.findAllStatusByName(statusName);
            Integer statusid=0;
            if(allStatusByName==null){
                InvestStatus investStatus = new InvestStatus(statusName);
                int insert = investStatusMapper.insert(investStatus);
                InvestStatus allStatusByName1 = investStatusMapper.findAllStatusByName(statusName);
                Integer statusId = allStatusByName1.getStatusId();
                statusid=statusId;
            }else {
                Integer statusId = allStatusByName.getStatusId();
                statusid=statusId;
            }
            //判断增加风控等级
            InvestRisk investRisk = new InvestRisk(riskPingname);
            int insert = investRiskMapper.insert(investRisk);
            InvestRisk allNewOne = investRiskMapper.findAllNewOne();
            Integer riskId = allNewOne.getRiskId();

            Invest invest1 = new Invest( investNumber,  investName,  investStartpay,  investGetyearmoney,
                    investDay,  investAmount,  investDeadline,  investReleasetime,  investPaytime,  investStafguard,
                    investBalance,  investSubsribe,  investRemaindertime,  investGetmonofyear,
                    null,  null,  null,  null,  null, null);

        }else {
            //如果项目已经有了,那么就修改项目信息
            Integer repayId = invest.getRepayId();
            Integer stafId = invest.getStafId();
            Integer companyId = invest.getCompanyId();
            Integer riskId = invest.getRiskId();
            Integer afterloanId = invest.getAfterloanId();//
            Integer statusId = invest.getStatusId();
            Integer typeId = invest.getTypeId();
            Invest invest1 = new Invest( investId,  investNumber,  investName,  investStartpay,  investGetyearmoney,  investDay,  investAmount,  investDeadline,  investReleasetime,  investPaytime,  investStafguard,  investBalance,  investSubsribe,  investRemaindertime,  investGetmonofyear,  repayId,  stafId,  companyId,  riskId,  afterloanId,  statusId, typeId);
            //修改项目信息
            investMapper.updateByPrimaryKey(invest1);
            //修改还款方式
            InvestRepay investRepay = new InvestRepay(repayId,repayName);
            investRepayMapper.updateByPrimaryKey(investRepay);
            //修改保障机构
            InvestStafguard investStafguard1 = new InvestStafguard(stafId,stafguardName);
            investStafguardMapper.updateByPrimaryKey(investStafguard1);
            //修改公司名字
            investCompanyMapper.updateCompanyNameById(companyId,companyName);
            //修改风控信用评分
            investRiskMapper.updateRiskPingNameById(riskId,riskPingname);
            //修改项目类型
            InvestType investType = new InvestType(typeId,typeName);
            investTypeMapper.updateByPrimaryKey(investType);
            //修改项目状态
            InvestStatus investStatus = new InvestStatus(statusId,statusName);
            investStatusMapper.updateByPrimaryKey(investStatus);
        }



    }
}
